Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
Axios + Express | Empty req.body (solved)

// START EIDT:

Solution was (thanks to mr. Hawkeye @robertklep) the name "body" in my post request in axios on the client side. Apparently you cannot name the body in axios post request anything but data, even though it's a reference passed from a parameter or similar.

// END EIDT:

I'm currently building an application with React as the frontend and Express as backend.

For API calls I'm using axios. My APIhas been working fine, but all of the sudden with no obvious change my post request body is not being sent to the backend.

In my index.js I had the following:

app.use(bodyParser.json());

but also tried with

app.use(express.json())

Client side function calling my post request:

 const checkForExistingUserData = async () => {
        if (user) {
            const body = {
                caseId: user['caseId']
            }

            await postRequest({request: body}, commonApiLocations["userDataLookUp"])
    }
}

My client side call is being call this way:

export function postRequest(body, url) {

        console.log(body) // Logs correct body with my data inside

        const config = {
        method: 'post',
        url: masterEndPoints['expressEndPoint'] + url,
        headers: {
            'Content-Type': 'application/json',
        },
        body // should be called "data" (see edit in top)
    };
    return axios(config);
}

Express route endpoint function:

route.post('/api/users/checkExistingUserData', async (req, res) => {

    try {
        const request = req.body.request;

        console.log("request: ", request); // Gives undefined

        if (request['caseId']) {
            let result = await checkIfExist(tableNames['TableName'], {'caseId': request.caseId})
            res.status(200).send(result);
        } else {
            res.status(200).send(false)
        }
    } catch (error) {
        res.status(500).send('En error occurred on the server')
    }
});

When I console.log on on the client side I see my request with the data that I expect to see.

When I console.log on the server I get an undefined.

When I console.log req.body I get "{}"

Any reasons for this error? I'm completely dumbfounded by this.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda